package cn.company.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.company.database.DBUtils;

/**
 * 控制后台业务逻辑的servlet
 * @author mzba
 *
 */
public class OurHomeControlServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		
		/*添加现有客户*/
		if(action.equals("addholdclient")){
			String clientname = request.getParameter("clientname");
			String imagepath = request.getParameter("path");
			String describe = request.getParameter("content");
			String taxis = request.getParameter("taxis");
			String url = request.getParameter("url");
			String[] sort = request.getParameterValues("type");
			String sortid = "";
			for (String id : sort) {
				sortid += id + ";";
			}
			
			int result = DBUtils.addHoldClient(clientname, imagepath, describe, url, sortid, taxis);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*修改现有客户*/
		else if(action.equals("updateholdclient")){
			int ids = Integer.parseInt(request.getParameter("id"));
			String clientname = request.getParameter("clientname");
			String imagepath = request.getParameter("path");
			String content = request.getParameter("content");
			String url = request.getParameter("url");
			String[] sort = request.getParameterValues("type");
			String sortid = "";
			for (String id : sort) {
				sortid += id + ";";
			}
			int taxis = Integer.parseInt(request.getParameter("taxis"));
			int result = DBUtils.updateHoldClient(ids, taxis, clientname, imagepath, content, url, sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*添加产品服务*/
		else if(action.equals("addproductservice")){
			String topic = request.getParameter("topic");
			String content = request.getParameter("content");
			String imagepath = request.getParameter("path");
			boolean isSecondTree = false;
			if (request.getParameter("secondMenu") != null && !request.getParameter("secondMenu").equals("")) {
				isSecondTree = true;
			}
			int sortid = Integer.parseInt(request.getParameter("sortid"));
			int result = DBUtils.addProduceService(topic, imagepath, content, isSecondTree, sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*修改产品服务*/
		else if(action.equals("updateproductservice")){
			int ids = Integer.parseInt(request.getParameter("id"));
			String topic = request.getParameter("topic");
			String content = request.getParameter("content");
			String imagepath = request.getParameter("path");
			int sortid = Integer.parseInt(request.getParameter("sortid"));
			int result = DBUtils.updateProductService(ids, topic, imagepath, content,sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*添加关于我们*/
		else if(action.equals("addaboutus")){
			String topic = request.getParameter("topic");
			String content = request.getParameter("content");
			int sortid = Integer.parseInt(request.getParameter("sortid"));
			String imagepath = request.getParameter("path");
			int result = DBUtils.addAboutUs(topic, content, imagepath, sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*修改关于我们*/
		else if(action.equals("updateaboutus")){
			int ids = Integer.parseInt(request.getParameter("id"));
			String topic = request.getParameter("topic");
			String content = request.getParameter("content");
			int sortid = Integer.parseInt(request.getParameter("sortid"));
			String imagepath = request.getParameter("path");
			int result = DBUtils.updateAboutUs(ids, topic, content, imagepath, sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*添加分类*/
		else if(action.equals("addsort")){
			int moduleid = Integer.parseInt(request.getParameter("module"));
			String sortname = request.getParameter("sortname");
			int result = DBUtils.addSort(sortname, moduleid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		
		/*修改分类*/
		else if(action.equals("updateSort")){
			int id = Integer.parseInt(request.getParameter("id"));
			int moduleid = Integer.parseInt(request.getParameter("module"));
			String sortname = request.getParameter("sortname");
			int result = DBUtils.updateSort(sortname, moduleid, id);
			if(result > 0){
				request.getRequestDispatcher("OurHome/sort/addsort.jsp").forward(request, response);
			}
		}
		
		/*添加解决方案*/
		else if(action.equals("addscheme")){
			String topic = request.getParameter("topic");
			String content = request.getParameter("content");
			int sortid = Integer.parseInt(request.getParameter("sortid"));
			String imagepath = request.getParameter("path");
			String longtopic = request.getParameter("longtopic");
			int result = DBUtils.addScheme(topic, longtopic, imagepath, content, sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*修改解决方案*/
		else if(action.equals("updatescheme")){
			int ids = Integer.parseInt(request.getParameter("id"));
			String topic = request.getParameter("topic");
			String content = request.getParameter("content");
			int sortid = Integer.parseInt(request.getParameter("sortid"));
			String imagepath = request.getParameter("path");
			String longtopic = request.getParameter("longtopic");
			int result = DBUtils.updateScheme(ids, topic, longtopic, imagepath, content, sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*添加企业文化*/
		else if(action.equals("addculture")){
			String topic = request.getParameter("topic");
			String content = request.getParameter("content");
			int sortid = Integer.parseInt(request.getParameter("subsortid"));
			String time = request.getParameter("time");
			String imagepath = request.getParameter("path");
			int result = DBUtils.addCulture(topic, imagepath, content, time, sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*修改企业文化*/
		else if(action.equals("updateculture")){
			int ids = Integer.parseInt(request.getParameter("id"));
			String topic = request.getParameter("topic");
			String content = request.getParameter("content");
			int sortid = Integer.parseInt(request.getParameter("subsortid"));
			String imagepath = request.getParameter("path");
			String time = request.getParameter("time");
			int result = DBUtils.updateCulture(ids, topic, imagepath, content, time,sortid);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		/*添加广告*/
		else if(action.equals("addadvertising")){
			String name = request.getParameter("name");
			String imagepath = request.getParameter("path");
			String type = request.getParameter("type");
			int result = DBUtils.addAdvertising(name, imagepath, type);
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
		
		/*添加企业文化子分类*/
		else if(action.equals("addsubsort")){
			String subsortname = request.getParameter("subsortname");
			int sortid = Integer.parseInt(request.getParameter("sortid"));
			int result = DBUtils.addSubSort(sortid, subsortname);	
			if(result > 0){
				request.getRequestDispatcher("OurHome/index.jsp").forward(request, response);
			}
		}
	}
}
